package com.airycode.distribution.distribution1.base;

import com.airycode.distribution.distribution1.constants.BusinessCode;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 接口层返回数据格式
 * @author admin
 * @date 2017/8/15
 */
public class ResponseData implements Serializable {

    private static final String RESULT = "result";

    private static final String LIST = "list";

    private static final long serialVersionUID = 4062778171116210522L;

    private int code = BusinessCode.SUCCESS.getCode();

    private String msg = BusinessCode.SUCCESS.getMsg();

    private Object data;

    public static ResponseData convertFromResult(BaseResult result) {
        ResponseData responseData = new ResponseData();
        responseData.setCode(result.getCode());
        responseData.setMsg(result.getMsg());

        if (result instanceof ModelResult) {
            Map<String, Object> map = new HashMap<>();
            map.put(RESULT, ((ModelResult) result).getResult());
            responseData.setData(map);
            return responseData;
        }
        if (result instanceof ListResult) {
            Map<String, Object> map = new HashMap<>();
            map.put(LIST, ((ListResult) result).getList());
            responseData.setData(map);
            return responseData;
        }
        if (result instanceof PageResult) {
            responseData.setData(((PageResult) result).getPage());
        }

        return responseData;
    }

    public Date getServerTime() {
        return new Date();
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
